package com.songlanyun.modules.rabbitmq.mq.consumer;

import com.rabbitmq.client.Channel;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.service.SendPrivateMessageMsgService;
import com.songlanyun.modules.rabbitmq.DelayedConstant;
import com.songlanyun.modules.rabbitmq.mq.producer.SendOrderOrRightsOrderMsgService;
import com.songlanyun.modules.ship.entity.OrderPackage;
import com.songlanyun.modules.ship.service.OrderPackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 普通自提订单过期不来拿货，开始执行自动收货
 */
@Component
@Slf4j
public class AutoPickupConsumer extends BaseConsumer {

    @Autowired
    private OrderPackageService orderPackageService;

    @Autowired
    private SendOrderOrRightsOrderMsgService sendOrderOrRightsOrderMsgService;

    @Autowired
    private SendPrivateMessageMsgService sendPrivateMessageMsgService;

    @Override
    protected void process(Message message) {
        Long packageId = ClassConvertUtils.objToLong(new String(message.getBody()));
        OrderPackage v = orderPackageService.getById(packageId);
        try {
            log.info(String.format("普通自提订单:%s,过期不来拿货，开始执行自动收货", v.getOrderCode()));
            orderPackageService.autoPickupReceipt(v);
            //发起自动封单
            sendOrderOrRightsOrderMsgService.receiptSuccessSendMsg(v.loadOrder());
            log.info(String.format("普通自提订单:%s,过期不来拿货，完成执行自动收货", v.getOrderCode()));

        } catch (Exception ex) {
            log.error(String.format("普通自提订单:%s,过期不来拿货，执行自动收货,出错%s", v.getOrderCode(), ex.getMessage()));
            throw ex;
        }

    }

    @RabbitListener(queues = DelayedConstant.OrderAutoPickupConstant.AUTO_PICKUP_DELAYED_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        super.handle(message, channel);
    }
}
